package com.example.tetrislogin;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;


import com.example.tetrislogin.databinding.ActivityMainBinding;
import com.example.tetrislogin.databinding.ActivityVarifyBinding;

import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

public class VerifyActivity extends AppCompatActivity {
    private ActivityVarifyBinding mBinding;
    private String verifiCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        mBinding=ActivityVarifyBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        EditText[] editTexts = {
                mBinding.varify1,
                mBinding.varify2,
                mBinding.varify3,
                mBinding.varify4,
                mBinding.varify5,
                mBinding.varify6
        };
        setupEditTextFocusChange(editTexts);
        mBinding.login.setOnClickListener(v -> {
            BmobSMS.verifySmsCode(MainActivity.phoneNumber, verifiCode, new UpdateListener() {
                @Override
                public void done(BmobException e) {
                    if (e == null) {
                        Toast.makeText(VerifyActivity.this, "验证成功", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(VerifyActivity.this,MainActivity.class);
                        startActivity(intent);
                    } else {
                        Toast.makeText(VerifyActivity.this, "验证失败", Toast.LENGTH_SHORT).show();
                        e.printStackTrace();
                    }
                }
            });
        });
    }
    //输入完跳转到下一格
    private void setupEditTextFocusChange(EditText[] editTexts) {
        for (int i = 0; i < editTexts.length; i++) {
            final int currentIndex = i;
            editTexts[i].addTextChangedListener(new TextWatcher() {
                //输入前
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

                //输入中
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (s.length() == 1) {
                        // 自动跳转到下一个输入框
                        if (currentIndex < editTexts.length - 1) {
                            editTexts[currentIndex + 1].requestFocus();
                            editTexts[currentIndex + 1].setBackgroundResource(R.drawable.varify_choice_shape);
                            editTexts[currentIndex].setBackgroundResource(R.drawable.varify_code_shape);
                        }else {
                            editTexts[currentIndex].setBackgroundResource(R.drawable.varify_code_shape);
                            hideKeyboard(VerifyActivity.this);
                            verifiCode=getVerificationCode(editTexts).trim();
                        }
                    } else if (s.length() == 0 && currentIndex > 0) {
                        // 删除时跳转到上一个输入框
                        editTexts[currentIndex - 1].requestFocus();
                        editTexts[currentIndex - 1].setBackgroundResource(R.drawable.varify_choice_shape);
                        editTexts[currentIndex].setBackgroundResource(R.drawable.varify_code_shape);

                    }
                }

                //满后
                @Override
                public void afterTextChanged(Editable s) {}
            });
        }
    }
    //隐藏键盘
    public static void hideKeyboard(Activity activity) {
        View view = activity.getCurrentFocus();
        if (view != null) {
            InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }
    //获取验证码
    private String getVerificationCode(EditText[] editTexts) {
        StringBuilder code = new StringBuilder();
        for (EditText et : editTexts) {
            code.append(et.getText().toString().trim());
        }
        return code.toString();
    }
}